/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.buanalintas.sandals.pos.bonussalesdetail;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Logger;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Persistence;
import com.buanalintas.bynga.formatter.MoneyFormatter;
import com.buanalintas.sandals.pos.entities.PosTransactionBonusSalesDetail;
import com.buanalintas.sandals.pos.entities.PosTransactionSale;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;

/**
 *
 * @author Developer
 */
public class BonusSalesDetail extends Page {

    Date date = new Date();
    private Field bonusSalesDetailId;
    private Field bonusSalesDetailBonusSalesId;
    private Field bonusSalesDetailSalesId;
    private Field bonusSalesDetailTotal;
    private Field bonusSalesDetailBonus;
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public BonusSalesDetail() {
        super();
        setTheClass(PosTransactionBonusSalesDetail.class);

        bonusSalesDetailId = new Field();
        bonusSalesDetailId.setName("bonusSalesDetailId");
        bonusSalesDetailId.setCaption("id");
        bonusSalesDetailId.setListProperties("");
        bonusSalesDetailId.setInputProperties("");
        getFields().add(bonusSalesDetailId);

        bonusSalesDetailBonusSalesId = new Field();
        bonusSalesDetailBonusSalesId.setName("bonusSalesDetailBonusSalesId");
        bonusSalesDetailBonusSalesId.setCaption("Bonus Sales");
        bonusSalesDetailBonusSalesId.setListProperties("");
        bonusSalesDetailBonusSalesId.setInputProperties("");
        getFields().add(bonusSalesDetailBonusSalesId);

        bonusSalesDetailSalesId = new Field();
        bonusSalesDetailSalesId.setName("bonusSalesDetailSalesId");
        bonusSalesDetailSalesId.setCaption("sales");
        bonusSalesDetailSalesId.setListProperties(" align=\"left\" width=\"30%\" nowrap");
        bonusSalesDetailSalesId.setInputProperties(" size=\"30\"");
        //DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //Date date = new Date();
        System.out.println("bro>>" + dateFormat.format(date));
        bonusSalesDetailSalesId.setSelectorCondition("t.salesId IN (SELECT t1.saleSalesId.salesId FROM PosTransactionSale t1 WHERE t1.saleDate='" + dateFormat.format(date) + "' )");
        bonusSalesDetailSalesId.getSelectorColumns().add(new String[]{"salesName", " Name", "width=\"20%\" nowrap"});
        bonusSalesDetailSalesId.setSelectorOrder("t.salesName");
        bonusSalesDetailSalesId.setRequired(true);
        getFields().add(bonusSalesDetailSalesId);

        bonusSalesDetailTotal = new Field();
        bonusSalesDetailTotal.setName("bonusSalesDetailTotal");
        bonusSalesDetailTotal.setCaption("total Sale");
        bonusSalesDetailTotal.setListProperties(" align=\"right\"  nowrap");
        bonusSalesDetailTotal.setInputProperties(" size=\"30\" ");
        bonusSalesDetailTotal.setFormater(MoneyFormatter.getInstance());
        bonusSalesDetailTotal.setRequired(true);
        getFields().add(bonusSalesDetailTotal);

        bonusSalesDetailBonus = new Field();
        bonusSalesDetailBonus.setName("bonusSalesDetailBonus");
        bonusSalesDetailBonus.setCaption("price bonus");
        bonusSalesDetailBonus.setListProperties(" align=\"right\" nowrap");
        bonusSalesDetailBonus.setInputProperties(" size=\"30\" ");
        bonusSalesDetailBonus.setFormater(MoneyFormatter.getInstance());
        bonusSalesDetailBonus.setRequired(true);
        getFields().add(bonusSalesDetailBonus);

        getFieldsAdd().clear();
        getFieldsAdd().add(bonusSalesDetailSalesId);
        //getFieldsAdd().add(bonusSalesDetailTotal);
        getFieldsAdd().add(bonusSalesDetailBonus);

        getFieldsEdit().clear();
        getFieldsEdit().add(bonusSalesDetailSalesId);
        getFieldsEdit().add(bonusSalesDetailTotal);
        getFieldsEdit().add(bonusSalesDetailBonus);

        getFieldsList().clear();
        getFieldsList().add(bonusSalesDetailSalesId);
        getFieldsList().add(bonusSalesDetailTotal);
        getFieldsList().add(bonusSalesDetailBonus);

        getActions().add(Action.TRANS_DETAIL_LIST);
        getActions().add(Action.TRANS_DETAIL_ADD);
        getActions().add(Action.TRANS_DETAIL_EDIT);

//        setController(new BonusSalesDetailController(this));
    }

    @Override
    public String validate(Object data) {
        EntityManager em = getEntityManager();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("ai 2>>");

        PosTransactionBonusSalesDetail bsDetail = (PosTransactionBonusSalesDetail) data;
        List<PosTransactionSale> sale = em.createQuery("SELECT t FROM PosTransactionSale t WHERE t.saleSalesId.salesId = " + bsDetail.getBonusSalesDetailSalesId().getSalesId() + " AND t.saleDate='" + dateFormat.format(date) + "'").setHint(Persistence.REFRESH_HINT, true).getResultList();
        
        BigDecimal totalSale = BigDecimal.ZERO;
        try {
            System.out.println("ai try>>");
            for (int i = 0; i < sale.size(); i++) {
                System.out.println("Sale Total :"+sale.get(i).getSaleTotal());
                totalSale = totalSale.add(sale.get(i).getSaleTotal());
                System.out.println("ai 3>>" + totalSale);
                
            }
            System.out.println("Sale Total :"+totalSale);
            bsDetail.setBonusSalesDetailTotal(totalSale);
        } catch (Exception e) {
            Logger.getInstance().log(e);
        }
        return null;
    }
//
//    @Override
//    public void afterInit(HttpServletRequest req, HttpServletResponse res) {
//        //b.setFormater(DateFormatter.getInstanceDate(this));
//        bonusSalesDetailBonusSalesId.getSelectorColumns().add(new String[]{"salesName", " Name", "width=\"30%\" nowrap"});
//        bonusSalesDetailBonusSalesId.setSelectorCondition("t.salesUnitId.unitId=" + getCurrentUnit().getUnitId());
//        getFields().add(bonusSalesDetailBonusSalesId);
//
//    }
//    @Override
//    public String doAction() {
//        try {
//            EntityManager em = getEntityManager();
//            HttpSession session = getRequest().getSession();
//            PosTransactionBonusSalesDetail bsDetail = new PosTransactionBonusSalesDetail();
//            System.out.println("0>> ");
//            List<PosTransactionBonusSalesDetail> list = ((List) session.getAttribute("transactions"));
//            PosTransactionSale sale = (PosTransactionSale) em.createQuery("SELECT t FROM PosTransactionSale t WHERE t.saleSalesId.salesId IN (SELECT t1.bonusSalesDetailSalesId.salesId FROM PosTransactionBonusSalesDetail t1 )").setHint(Persistence.REFRESH_HINT, true).getSingleResult();
//            boolean exists = false;
//            for (PosTransactionBonusSalesDetail sd : list) {
//                if (sd.getBonusSalesDetailSalesId().equals(sale.getSaleSalesId())) {
//                    System.out.println("1>> " + sale.getSaleSalesId());
//                    exists = true;
//                    break;
//                }
//            }
//            if (!exists) {
//                bsDetail.setBonusSalesDetailTotal(sale.getSaleTotal());
//                list.add(bsDetail);
//                System.out.println("2>> " + sale.getSaleTotal());
//            }
//        } catch (Exception e) {
//            Logger.getInstance().log(e);
//        }
//        return super.doAction();
//    }
//    class BonusSalesDetailController extends Controller {
//
//        public BonusSalesDetailController(Page page) {
//            super(page);
//        }
//
//        @Override
//        public void beforeInsertDetail(Object data) {
//            super.beforeInsertDetail(data);
//        }
//
//        @Override
//        public void beforeInsert(Object data) {
//            System.out.println("BI :: ");
//        }
//        
//        
//        
//        @Override
//        public void afterInsert(Object data) {
//            System.out.println("ai 1>>");
//            EntityManager em = getEntityManager();
//            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//            Date date = new Date();
//            System.out.println("ai 2>>");
//            PosTransactionBonusSalesDetail bsDetail = (PosTransactionBonusSalesDetail) data;
//            List<PosTransactionSale> sale = em.createQuery("SELECT t FROM PosTransactionSale t WHERE t.saleSalesId = " + bsDetail.getBonusSalesDetailSalesId()).setHint(Persistence.REFRESH_HINT, true).getResultList();
//
//            BigDecimal totalSale = BigDecimal.ZERO;
//            try {
//                System.out.println("ai try>>");
//                for (int i = 0; i < sale.size(); i++) {
//                    totalSale = totalSale.add(sale.get(i).getSaleTotal());
//                    System.out.println("ai 3>>" + totalSale);
//                }
//                bsDetail.setBonusSalesDetailTotal(totalSale);
//            } catch (Exception e) {
//                Logger.getInstance().log(e);
//            }
//        }
//    }
}
